如何使用 System.Text.Json 启用不区分大小写的属性名称 您所在的位置:网站首页 枚举 json序列号 名称 如何使用 System.Text.Json 启用不区分大小写的属性名称

如何使用 System.Text.Json 启用不区分大小写的属性名称

2024-02-14 09:43| 来源: 网络整理| 查看: 265

如何使用 System.Text.Json 启用不区分大小写的属性名称 项目 05/26/2023

本文将介绍如何使用 System.Text.Json 命名空间启用不区分大小写的属性名称匹配。

不区分大小写的属性匹配

默认情况下,反序列化会查找 JSON 与目标对象属性之间区分大小写的属性名称匹配。 若要更改该行为,请将 JsonSerializerOptions.PropertyNameCaseInsensitive 设置为 true:

注意

Web 默认值为不区分大小写。

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var weatherForecast = JsonSerializer.Deserialize(jsonString, options); Dim options As JsonSerializerOptions = New JsonSerializerOptions With { .PropertyNameCaseInsensitive = True } Dim weatherForecast1 = JsonSerializer.Deserialize(Of WeatherForecast)(jsonString, options)

下面是具有 camel 大小写属性名称的示例 JSON。 它可以反序列化为具有帕斯卡拼写法属性名称的以下类型。

{ "date": "2019-08-01T00:00:00-07:00", "temperatureCelsius": 25, "summary": "Hot", } public class WeatherForecast { public DateTimeOffset Date { get; set; } public int TemperatureCelsius { get; set; } public string? Summary { get; set; } } Public Class WeatherForecast Public Property [Date] As DateTimeOffset Public Property TemperatureCelsius As Integer Public Property Summary As String End Class 请参阅 System.Text.Json 概述 如何对 JSON 进行序列化和反序列化


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有